/*
 * @(#)ArdorSWTRenderer 	1.0 2010/09/30
 * 
 * Copyright 2010 guyven and David Croft. All rights reserved.
 */
package com.iawg.attrition.engine.renderer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Composite;

import com.ardor3d.framework.CanvasRenderer;
import com.ardor3d.framework.FrameHandler;
import com.ardor3d.framework.Scene;
import com.ardor3d.framework.lwjgl.LwjglCanvasRenderer;
import com.ardor3d.framework.swt.SwtCanvas;
import com.ardor3d.input.PhysicalLayer;
import com.ardor3d.input.logical.LogicalLayer;
import com.ardor3d.input.swt.SwtFocusWrapper;
import com.ardor3d.input.swt.SwtKeyboardWrapper;
import com.ardor3d.input.swt.SwtMouseWrapper;
import com.ardor3d.renderer.Camera;


/** 
 * This class provides a link to an outside graphical rendering engine.
 * 
 * @version 1.0 2010/09/30
 * @author David Croft
 */
public class ArdorSWTRenderer extends BasicRenderer{

	public ArdorSWTRenderer() {
		
	}	

	public void getArdorLWJGLCanvasRenderer(Composite parent, Scene scene, FrameHandler frameWork) {
	       // Add a new tab to hold our canvas
//      final TabItem item = new TabItem(tabFolder, SWT.NONE);
//      item.setText("Canvas #");
//      tabFolder.setSelection(item);
		final Composite canvasParent = new Composite(parent, SWT.NONE);
		canvasParent.setLayout(new FillLayout());
//      item.setControl(canvasParent);

		final GLData data = new GLData();
		data.depthSize = 8;
		data.doubleBuffer = true;

		canvasParent.layout();

		final SwtCanvas canvas = new SwtCanvas(canvasParent, SWT.NONE, data);
		LwjglCanvasRenderer lwjglCanvasRenderer = new LwjglCanvasRenderer(scene);
		canvas.setCanvasRenderer(lwjglCanvasRenderer);
		frameWork.addCanvas(canvas);
		canvas.addControlListener(newResizeHandler(canvas, lwjglCanvasRenderer));
		canvas.setFocus();

		final LogicalLayer logicalLayer = new LogicalLayer();

		final SwtKeyboardWrapper keyboardWrapper = new SwtKeyboardWrapper(canvas);
		final SwtMouseWrapper mouseWrapper = new SwtMouseWrapper(canvas);
		final SwtFocusWrapper focusWrapper = new SwtFocusWrapper(canvas);

		final PhysicalLayer pl = new PhysicalLayer(keyboardWrapper, mouseWrapper, focusWrapper);

      // pl.init();

      // canvas.addKeyListener(keyboardWrapper);
      // mouseWrapper.listenTo(canvas);

		logicalLayer.registerInput(canvas, pl);
	}
	
	// So that the size of the c
    static ControlListener newResizeHandler(final SwtCanvas swtCanvas, final CanvasRenderer canvasRenderer) {
        final ControlListener retVal = new ControlListener() {
            public void controlMoved(final ControlEvent e) {}

            public void controlResized(final ControlEvent event) {
                final Rectangle size = swtCanvas.getClientArea();
                if ((size.width == 0) && (size.height == 0)) {
                    return;
                }
                final float aspect = (float) size.width / (float) size.height;
                final Camera camera = canvasRenderer.getCamera();
                if (camera != null) {
                    final float fovY = 45; // XXX no camera.getFov()
                    final double near = camera.getFrustumNear();
                    final double far = camera.getFrustumFar();
                    camera.setFrustumPerspective(fovY, aspect, near, far);
                    camera.resize(size.width, size.height);
                }
            }
        };
        return retVal;
    }
}
